<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Model {

    
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
        
    function getAllUser()
    {
        $query = $this->db->get_where('ss_user', array('enabled' => 1));
        
        return $query->result();
    }

    function get_user($userid){
    	
    	$query = $this->db->get_where('ss_user', array('userid' => $userid));
    	
    	return $query->result();
    	
    }

    function create_user($loginname,$password,$name,$surname,$email,$role,$enabled)
    {
    	$this->loginname = $loginname; // please read the below note
    	$this->password = $password;
    	$this->name = $name;
    	$this->surname = $surname;
    	$this->email = $email;
    	$this->role = $role;
    	$this->enabled = $enabled;
    	$this->create_date = date("Y-m-d H:i:s");
    
    	$this->db->insert('ss_user', $this);
    }
    
    function edit_user($userid,$password,$name,$surname,$email,$role,$enabled)
    {
    	
    	if($password != ""){
    		$this->password = $password;
    	}
    	
    	$this->name = $name;
    	$this->surname = $surname;
    	$this->email = $email;
    	$this->role = $role;
    	$this->enabled = $enabled;
    
    	$this->db->update('ss_user', $this, array('userid' => $userid));
    }
    
    function delete_user($userid){
    	$this->db->delete('ss_user', array('userid' => $userid));
    }
    
    public function authenUser($loginname,$password){
    	$key = config_item('encryption_key');
    	 
    	$query = $this->db->get_where('ss_user', array('loginname' => $loginname,'password' => md5($password.$key)));
    	
    	$user = $query->result();
    	
    	if (count($user) == 1){
    		return true;	
    	}else{
    	  	return false;
    	}
    }
    
    
    /*function insert_entry()
    {
        $this->title   = $_POST['title']; // please read the below note
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->insert('entries', $this);
    }

    function update_entry()
    {
        $this->title   = $_POST['title'];
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->update('entries', $this, array('id' => $_POST['id']));
    }
*/
}